package com.ruoyi.project.task.showInfo.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.person.info.domain.PersonInfo;
import com.ruoyi.project.pro.picture.domain.ProPicture;
import com.ruoyi.project.pro.upload.FileUpLoadUtil;
import com.ruoyi.project.task.mission.domain.TaskMission;
import com.ruoyi.project.task.mission.domain.TaskMissionVo;
import com.ruoyi.project.task.mission.service.ITaskMissionService;
import com.ruoyi.project.task.showInfo.service.IShowInfoService;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

import static com.ruoyi.framework.web.domain.AjaxResult.error;

/**
 * @author Pathosis
 * @date 2020-09-20 14:59
 * @description:
 */
@Controller
@RequestMapping("/task/showInfo")
public class ShowInfoController extends BaseController {

    @Autowired
    private ITaskMissionService itms;

    @Autowired
    private IShowInfoService iss;

    private String perfix = "/task/showInfo";

    @RequiresPermissions("task:showInfo:view")
    @GetMapping
    public String toShowinfo() {
        return perfix + "/showInfo";
    }

    @RequiresPermissions("task:showInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TaskMissionVo taskMissionVo)
    {
        //分页
        startPage();

        List<TaskMissionVo> list = iss.selectTaskMissionList(taskMissionVo);

        return getDataTable(list);
    }
    /**
     * 回显需要修改的任务
     */
    @GetMapping("/edit/{missionId}")
    public String edit(@PathVariable("missionId") Long missionId, ModelMap mmap) {
        mmap.addAttribute("taskMission", itms.selectTaskMissionById(missionId));
        //查询出所有的员工
        List<PersonInfo> personInfos = itms.selectAllPerson();
        mmap.addAttribute("list", personInfos);
        return perfix + "/edit";
    }


    /**
     * 修改任务
     */
    @RequiresPermissions("task:showInfo:edit")
    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated TaskMission taskMission) {
        return toAjax(itms.updateTaskMission(taskMission));
    }

    @RequestMapping("/addPicture")
    @ResponseBody
    public AjaxResult fileUpLoad(@RequestParam("file_data") MultipartFile file,String missionId){
        String fileName = FileUpLoadUtil.upLoadFile(file);
        System.out.println("上传成功");
        TaskMission taskMission = new TaskMission();
        taskMission.setMissionId(Long.valueOf(missionId));
        taskMission.setUrl("https://tcr-5cb5kifq-1302869269.cos.ap-guangzhou.myqcloud.com/"+fileName);
        return toAjax(itms.updateTaskMission(taskMission));
    }

}
